#!/bin/bash

echo "🔧 Mask Token Starter 依赖修复脚本"
echo "=================================="

# 1. 清理项目
echo "1. 清理项目..."
mvn clean -q

# 2. 检查依赖
echo "2. 检查依赖冲突..."
mvn dependency:tree -q | grep -E "(spring-boot|spring-security)" || echo "未发现明显冲突"

# 3. 重新编译
echo "3. 重新编译项目..."
mvn compile -q
if [ $? -eq 0 ]; then
    echo "✅ 编译成功"
else
    echo "❌ 编译失败"
    exit 1
fi

# 4. 重新打包
echo "4. 重新打包项目..."
mvn package -DskipTests -q
if [ $? -eq 0 ]; then
    echo "✅ 打包成功"
else
    echo "❌ 打包失败"
    exit 1
fi

# 5. 重新安装到本地仓库
echo "5. 重新安装到本地仓库..."
mvn install -DskipTests -q
if [ $? -eq 0 ]; then
    echo "✅ 安装成功"
else
    echo "❌ 安装失败"
    exit 1
fi

# 6. 检查JAR包内容
echo "6. 检查JAR包内容..."
if jar -tf target/mask-token-spring-boot-starter-1.0.0.jar | grep -q "Result.class"; then
    echo "✅ Result类已包含在JAR包中"
else
    echo "❌ Result类未找到"
    exit 1
fi

# 7. 测试示例项目
echo "7. 测试示例项目..."
cd example
mvn clean compile -q
if [ $? -eq 0 ]; then
    echo "✅ 示例项目编译成功"
else
    echo "❌ 示例项目编译失败"
    exit 1
fi

cd ..

echo ""
echo "🎉 依赖修复完成！"
echo ""
echo "如果仍然遇到 NoClassDefFoundError 错误，请检查："
echo "1. 确保项目依赖已正确引入"
echo "2. 检查是否存在版本冲突"
echo "3. 清理IDE缓存并重新导入项目"
echo "4. 查看故障排除指南: TROUBLESHOOTING.md"
